﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Accounts
{
    /// <summary>
    /// Interaction logic for TransactionHistory.xaml
    /// </summary>
    public partial class TransactionHistory : Window
    {
        private tblAccount _account;
        private CollectionViewSource _transactionViewSource;
        private CollectionViewSource _currencyViewSource;
        private SalesManagerEntities _sme;
        private Int32 _pageStart;

        public TransactionHistory(SalesManagerEntities sme, tblAccount account)
        {
            InitializeComponent();

            _account = account;
            _sme = sme;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _transactionViewSource = (CollectionViewSource)FindResource("TransactionViewSource");
            _currencyViewSource = (CollectionViewSource)FindResource("CurrencyViewSource");

            _currencyViewSource.Source = from c in _sme.codeCurrency select c;
            LoadList();
        }

        private void LoadList()
        {
            _transactionViewSource.Source = (from c in _sme.tblTransactions.Include("TransactionDetails.Account")
                                                .Include("TransactionType")
                                                .Include("TransactionStatus")
                                                .Include("TransactionResponse")
                                             where c.TransactionDetails.Any(t => t.AccountID == _account.AccountID)
                                             orderby c.CreateDate descending
                                             select c).Skip(_pageStart).Take(200);

            btnPrev.IsEnabled = (_pageStart > 0);
            btnNext.IsEnabled = ((BindingListCollectionView)_transactionViewSource.View).Count == 200;
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            _pageStart += 200;
            LoadList();
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            _pageStart -= 200;
            LoadList();
        }
    }
}
